package com.dushiguang.findhoney.View.ConditionFilterView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dushiguang.findhoney.R;
import com.dushiguang.findhoney.Utils.Iconfont.SetIconFont;
import com.dushiguang.findhoney.Utils.UIUtil.UIUtil;

import java.util.ArrayList;

/**
 * Created by ex-dushiguang201 on 2015-12-08.
 */
public class ConditionFilterView extends LinearLayout {
    private Context context;
    private int textSize = -1;
    private int textColor = -1;
    private static final int ONE_LIST_ITEM = 1;//只有一级列表
    private static final int TWO_LIST_ITEM = 2;//有二级列表
    private static final int MORE_LIST_ITEM = 3;//有多级列表

    private static final int UN_SELECT = -1;//没有被选择



    private int mTextViewConditionPadding = 30;//条件筛选的TextView的Padding值
    private int popwindowHeight = 0;

    public ConditionFilterView(Context context) {
        super(context);
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        init(context);
    }

    public ConditionFilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ConditionFilterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context) {
        this.context = context;
        mTextViewConditionPadding = UIUtil.dip2px(context, 12);

        popwindowHeight = popwindowHeight >= 900 ? 900 : popwindowHeight;
        setGravity(Gravity.CENTER_VERTICAL);
        removeAllViews();
    }

    public ConditionFilterView addConditionCategory(String categoryName, ArrayList<ConditionItem> conditionItems) {
        addConditionCategory(categoryName, conditionItems, UN_SELECT, "", false);
        return this;
    }

    /**
     * 在View上添加筛选条件
     * @param categoryName
     * @param conditionItems
     * @param defaultId
     * @param defChildValue
     * @param isKeepGroupCategoryName
     * @return
     */
    public ConditionFilterView addConditionCategory(String categoryName,
                                                    ArrayList<ConditionItem> conditionItems,
                                                    int defaultId,
                                                    String defChildValue,
                                                    boolean isKeepGroupCategoryName) {

        ListCategoryBuilder builder = new ListCategoryBuilder();
        builder.mCategoryName = categoryName;
        builder.mConditionList = conditionItems;
        builder.mDefId = defaultId;
        builder.mDefValue = defChildValue;
        builder.mIsKeepCategoryName = isKeepGroupCategoryName;
        addCategory(builder);
        return this;
    }

    private void addCategory(ListCategoryBuilder builder) {

        LayoutParams conditionFilterViewLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);

        RelativeLayout conditionFilterView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_filter_category, null);
        conditionFilterView.setLayoutParams(conditionFilterViewLp);

        TextView categoryName = (TextView) conditionFilterView.findViewById(R.id.category_name_tv);
        TextView categoryIcon = (TextView) conditionFilterView.findViewById(R.id.category_ic_tv);

        if (textSize != -1) {
            categoryName.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        }

        if (textColor != -1) {
            categoryName.setTextColor(textColor);
            categoryIcon.setTextColor(textColor);
        }

        categoryName.setText(builder.mCategoryName);

        //TODO 优化IConFont的获得方式
        categoryIcon.setText(context.getResources().getString(R.string.ic_down));
        SetIconFont.initIconFont(context, categoryIcon);

        categoryName.setTag(builder);

        if (getChildCount() == 0) {
            conditionFilterView.findViewById(R.id.split_line).setVisibility(View.GONE);
        }

        addView(conditionFilterView);
    }

    private OnClickListener onCategoryClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            ListCategoryBuilder builder = (ListCategoryBuilder) view.findViewById(R.id.category_name_tv).getTag();


        }
    };

    /**
     * 筛选条件
     */
    public static class ConditionItem {
        private int codeId;//筛选条件的ID
        private String value;

        public ConditionItem(int codeId, String value) {
            this.codeId = codeId;
            this.value = value;
        }

        public int getCodeId() {
            return codeId;
        }

        public void setCodeId(int codeId) {
            this.codeId = codeId;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    public static class ListCategoryBuilder {
        protected int                      mCategoryType       = ONE_LIST_ITEM;
        protected int                      mPopupWindowHeight;
        protected int                      mDefIndex           = UN_SELECT;
        protected int                      mDefId              = UN_SELECT;
        protected String                   mDefValue;
        protected String                   mCategoryName;
        protected boolean                  mIsKeepCategoryName = false;
        protected boolean                  mIsReset            = false;
        protected ArrayList<ConditionItem> mConditionList;
        protected boolean                  isAddSubway;
    }


}
